package cn.wolfcode.luowowo.job;

import cn.wolfcode.luowowo.cache.Util.RedisKeys;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVORedisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVORedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * redis数据持久化
 */
@Component
public class RedisDataPersistenceJob {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelsService travelsService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;
    @Autowired
    private ITravelStatisVORedisService travelStatisVORedisService;
    //秒 分 小时 月份中的日期 月份 星期中的日期 年份
    //0 0 0 1/1 * ? 
    @Scheduled(cron="0 0/10 * * * ? ")
    public void redisDataPersistence(){
        System.out.println(new Date());
        //将所有攻略vo中的数据存入MongoDB数据库中
        //1.获取所有攻略的vo对象
        String pattern = RedisKeys.STRATEGY_STATIS_VO.join("*");
        List<StrategyStatisVO> list = strategyStatisVORedisService.list(pattern);
        //获取到每一个vo对象,
        for (StrategyStatisVO strategyStatisVO : list) {
            //再进行更新操作
            strategyService.updateVO(strategyStatisVO);
        }
        //将所有游记vo中的数据存入MongoDB数据库中
        //1.获取所有攻略的vo对象
        String pattern2 = RedisKeys.TRAVEL_STATIS_VO.join("*");
        List<TravelStatisVO> list2 = travelStatisVORedisService.list(pattern2);
        //获取到每一个vo对象,
        for (TravelStatisVO travelStatisVO : list2) {
            //再进行更新操作
            travelsService.updateVO(travelStatisVO);
        }
    }
}